﻿#include "ossIni.h"
#include <stdio.h>
#include <string.h>

void Test1()
{
	Handle hini = OssOpenIni("./test.ini");

	if (hini == NULL)
	{
		printf("ini初始化失败");
		return;
	}

	do 
	{
		OssIniSetInt(hini,"wjh","lumei",123);

		int lumei = OssIniGetInt(hini,"wjh","lumei",1123);

		OssIniSetString(hini,"lumei","wjh","922");

		char wjhvalue[20];
		OssIniGetString(hini,"lumei","wjh","aaaa",wjhvalue,20);


		if (lumei != 123)
		{
			printf("OssIniSetInt or OssIniGetInt error\n");
		}

		if (strcmp(wjhvalue,"922") != 0)
		{
			printf("OssIniGetString or OssIniSetString err\n");
		}
	} while (false);

	OssCloseIni(hini);

	//关闭后测试
	hini = OssOpenIni("./test.ini");
	if (hini == NULL)
	{
		printf("ini初始化失败  2");
		return;
	}

	int lumei = OssIniGetInt(hini,"wjh","lumei",1123);
	char wjhvalue[20];
	OssIniGetString(hini,"lumei","wjh","aaaa",wjhvalue,20);

	if (lumei != 123)
	{
		printf("OssIniSetInt or OssIniGetInt error2\n");
	}

	if (strcmp(wjhvalue,"922") != 0)
	{
		printf("OssIniSetString or OssIniGetString err2\n");
	}

	OssIniSetString(hini,"wjhlumei","daughter","吴堃瑶");

    //OssPrintIniContext(hini);
	OssCloseIni(hini);
	printf("test end\n");
}

void TestPrivateProfileApi()
{
    char* iniPath = "./wjhlumei.ini";
    //OssWritePrivateProfileInt("wjh","daugtherAge",2,iniPath);
    //OssWritePrivateProfileString("wjh","wife","卢梅",iniPath);
    //OssWritePrivateProfileString("wjhlumei","daugtherName","吴堃瑶",iniPath);

	char acTest[40];
	Bool bRslt = FALSE;
	int iRslt = 0;
	for (int i = 1; i<65535; i++)
	{
		memset(acTest, 0, sizeof(acTest));
		sprintf(acTest, "tac%d", i);

		bRslt = OssWritePrivateProfileInt("ceshi", acTest, i, iniPath);
		if (bRslt)
		{
			printf("OssWritePrivateProfileInt success while i=%d\n",i);

			iRslt = OssGetPrivateProfileInt("ceshi", acTest, 0, iniPath);
			if (i != iRslt)
			{
				break;
			}
			//printf("OssGetPrivateProfileInt is %d, while i=%d\n",iRslt, i);
			continue;
		}
		else
		{
			printf("OssWritePrivateProfileInt failed while i=%d\n",i);
			printf("error end\n");
			break;
		}
	}
	
	printf("test end\n");

    //int iAge;

    //iAge = OssGetPrivateProfileInt("wjh","daugtherAge",3,iniPath);

    //char myWife[20];
    //if (!OssGetPrivateProfileString("wjh","wife","卢",myWife,20,iniPath))
    //{
    //    printf("error\n");
    //}

    //printf("age=%d，wife=%s\n",iAge,myWife);

    ////OssWritePrivateProfileInt("wjh","wife",2,"wjhlumei.ini");
}

/**
 *      打印解析后的ini文件输出
 */
void TestPrintIniFile()
{
    Handle hini = OssOpenIni("./lteOmc.ini");
    if (hini == NULL)
    {
        printf("ini初始化失败");
        return;
    }

    OssPrintIniContext(hini);

    OssCloseIni(hini);
}

void TestGetString()
{
    char *pcConfigFilePath = "LteMaster.ini";
    char m_acIp[30];
    DWord m_dwPort;

    OssGetPrivateProfileString("SlaveServer", "ip", "127.0.0.1", m_acIp, sizeof(m_acIp)-1, pcConfigFilePath);
    m_dwPort = OssGetPrivateProfileInt("SlaveServer", "port", 3000, pcConfigFilePath);

    printf("ip=%s\n",m_acIp);
	printf("port=%d\n",m_dwPort);
}

void TestUtf8Ini()
{
    char *pcConfigFilePath = "ManageDataMaster.ini";

    int m_iCreateFileInterval = OssGetPrivateProfileInt("manageDataMaster", "createFileInterval", 60, pcConfigFilePath);


	printf("m_iCreateFileInterval=%d\n",m_iCreateFileInterval);
    
}

void TestWriteIni()
{
    OssWritePrivateProfileString("wjh","lumei","wky","./wjhlumei.ini");
}

int main(int argc, char* argv[])
{
    //TestPrintIniFile();
	//Test1();

    //TestPrivateProfileApi();
    TestWriteIni();

    //TestGetString();
    //TestUtf8Ini();

    printf("测试完成\n");
	getchar();
	return 0;

}

